package interfaces.hud.pilot;

import java.util.LinkedList;

import logic.common.player.awards.Award;

import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.util.Color;

import interfaces.superWidgets.StaticContent;
import interfaces.superWidgets.UpdateWidget;

public class AwardContent extends StaticContent implements UpdateWidget {
	
	public static final float MAX_TIME = 3f;
	
	private LinkedList<Award> awards;
	private boolean isVisible;
	private Award currentAward;
	private AwardView awardView;
	private float currentTime;
	
	public AwardContent(int size, int x, int y) {
		super(size, size, x, y);
		
		getAppearance().add(new PlainBackground(Color.TRANSPARENT));
		
		awards = new LinkedList<Award>();
		
		setVisible(false);
		
		layout();
	}
	
	public void addAward(Award award) {
		awards.add(award);
		if(!isVisible) changeVisible(true);
	}
	
	@Override
	public void update(float time) {
		if(awards.isEmpty() && currentTime <= 0f) changeVisible(false);
		else {
			changeVisible(true);
			if(currentAward == null || currentTime <= 0f) {
				currentAward = awards.poll();
				currentTime = MAX_TIME;
				
				if(awardView != null) removeWidget(awardView);
				
				awardView = new AwardView(width, height, currentAward);
				awardView.changeTextColor(Color.WHITE);
				addWidget(awardView);
				layout();
			}
			assert(currentAward != null);
			
			currentTime -= time;
		}
	}
	
	private void changeVisible(boolean vis) {
		setVisible(vis);
	}
}